﻿---@diagnostic disable: trailing-space
local extension = Package:new("moepack_cardex", Package.CardPack)
extension.extensionName = "moepack"

Fk:loadTranslationTable{
  ["moepack_cardex"] = "萌包卡牌EX",
}

local U = require "packages/utility/utility"

local eagle_skill = fk.CreateActiveSkill{
  name = "eagle_skill",
  prompt = "#eagle_skill",
  mod_target_filter = function(self, to_select, selected, user)
    return user.id ~= to_select
  end,
  target_filter = Util.TargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local player = room:getPlayerById(effect.from)
    if not to:isKongcheng() then
      U.viewCards(player, to:getCardIds("h"), self.name, "$ViewCardsFrom:"..to.id)
    end
    room:addTableMark(player, "eagle-turn", to.id)
  end,
}

local eagle_distance = fk.CreateTargetModSkill{
  name = "#eagle_distance",
  global = true,
  bypass_distances = function(self, player, skill, card, to)
    return to and player and card and table.contains(player:getTableMark("eagle-turn"), to.id)
  end,
}
Fk:addSkill(eagle_distance)

local eagle = fk.CreateBasicCard{
  name = "eagle",
  suit = Card.Diamond,
  number = 4,
  skill = eagle_skill,
  special_skills = { "emo_zhujian&" },
} 

extension:addCards({
  eagle,
})

Fk:loadTranslationTable{
  ["eagle"] = "鹰",
	[":eagle"] = "基本牌<br /><b>时机</b>：出牌阶段<br /><b>目标：</b>一名其他角色<br /><b>效果：</b>观看目标角色手牌，本回合对其使用牌没有距离限制。",
  ["eagle_skill"] = "鹰",
  ["#eagle_skill"] = "观看一名角色手牌且本回合对其用牌无距离限制",
}

local poisoned_wine_trigger = fk.CreateTriggerSkill{
  name = "poisoned_wine_trigger",
  global = true,
  mute = true,
  priority = 0.01,
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@!poisoned_wine-turn") > 0 and data.card.trueName == "slash"
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@!poisoned_wine-turn", 0)
    data.disresponsiveList = table.map(player.room.alive_players, Util.IdMapper)
  end,
}
Fk:addSkill(poisoned_wine_trigger)

local poisoned_wine_skill = fk.CreateActiveSkill{
  name = "poisoned_wine_skill",
  target_num = 1,
  prompt = "#poisoned_wine_skill",
  mod_target_filter = function(self, to_select, selected, user, card)
    local to = Fk:currentRoom():getPlayerById(to_select)
    return to == user or to.hp == 1
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.to)
    if target.dead then return end
    if target == player then
      room:addPlayerMark(target, "@!poisoned_wine-turn")
    else
      room:loseHp(target, 1, self.name)
    end
  end
}
local poisoned_wine = fk.CreateBasicCard{
  name = "poisoned_wine",
  suit = Card.Spade,
  number = 9,
  skill = poisoned_wine_skill,
}

extension:addCards({
  poisoned_wine,
})

Fk:loadTranslationTable{
  ["poisoned_wine"] = "鸩",
	[":poisoned_wine"] = "基本牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：你/体力值为1的一名其他角色<br /><b>效果</b>：本回合下一张【杀】无法被响应/目标角色失去1点体力。",
  ["poisoned_wine_skill"] = "鸩",
  [":poisoned_wine_skill"] = "①出牌阶段，对自己使用，本回合下一张【杀】无法被响应。②出牌阶段，对体力值为1的一名其他角色使用，目标角色失去1点体力。",
  ["@!poisoned_wine-turn"] = "鸩",
  ["#poisoned_wine_skill"] = "选择你：你本回合下一张【杀】无法响应；选择其他角色：其失去1点体力",
}

local pearls_trigger = fk.CreateTriggerSkill{
  name = "pearls_trigger",
  global = true,
  mute = true,
  priority = 0.01, 
  frequency = Skill.Compulsory,
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:isWounded() and table.find(player:getCardIds("h"), function(id) return Fk:getCardById(id).name == "pearls" end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(player:getCardIds("h"), function(id) return Fk:getCardById(id).name == "pearls" end)
    if #cards == 0 then return end
    local id = table.random(cards)
    assert(type(id) == "number")
    room:broadcastPlaySound("./packages/moepack/audio/card/male/pearls")
    room:sendLog{
      type = "#PearlsEffect",
      card = {id},
      from = player.id,
    }
    room:moveCards({
      ids = {id},
      from = player.id,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonPutIntoDiscardPile,
      skillName = self.name,
    })
    room:recover({ who = player,  num = 1, skillName = "pearls_skill" , card = Fk:getCardById(id), })
  end,
}

Fk:addSkill(pearls_trigger)

local pearls_prohibit = fk.CreateProhibitSkill{
  name = "#pearls_prohibit",
  global = true,
  prohibit_discard = function(self, player, card)
    return card.name == "pearls" and table.contains(player.player_cards[Player.Hand], card.id)
  end,
}
Fk:addSkill(pearls_prohibit)

local pearls_skill = fk.CreateActiveSkill{
  name = "pearls_skill",
  mod_target_filter = Util.FalseFunc,
  can_use = Util.FalseFunc,
  on_effect = Util.FalseFunc,
}

local pearls = fk.CreateBasicCard{
  name = "pearls",
  suit = Card.Diamond,
  number = 10,
  skill = pearls_skill,
}

extension:addCards({
  pearls,
})

Fk:loadTranslationTable{
  ["pearls"] = "珠",
	[":pearls"] = "基本牌<br /><b>效果</b>：无法使用，无法主动弃置。每当你受到伤害后，若你已受伤，将一张【珠】置入弃牌堆并回复1点体力。",
  ["pearls_trigger"] = "珠",
  ["pearls_skill"] = "珠",
  ["#PearlsEffect"] = "%from 触发了 %card 的效果",
}


local hunting_skill = fk.CreateActiveSkill{
  name = "hunting_skill",
  prompt = "#hunting_skill",
  mod_target_filter = Util.TrueFunc,
  target_filter = Util.TargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    if to.dead then return end
    room:addPlayerMark(to, "@!hunting", 3)
  end
}

local hunting_trigger = fk.CreateTriggerSkill{
  name = "hunting_trigger",
  global = true,
  mute = true,
  priority = 0.1, 
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@!hunting") > 0 and data.from and not data.from.dead
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = math.min(data.damage, player:getMark("@!hunting"))
    room:removePlayerMark(player, "@!hunting", n)
    data.from:drawCards(n, self.name)
  end,
}
Fk:addSkill(hunting_trigger)

local hunting = fk.CreateTrickCard{
  name = "hunting",
  suit = Card.Club,
  number = 3,
  skill = hunting_skill,
  special_skills = { "emo_zhujian&" },
}

extension:addCards({
  hunting,
})

Fk:loadTranslationTable{
  ["hunting"] = "狩猎",
  [":hunting"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：一名角色<br /><b>效果</b>：目标角色受到1点伤害时，伤害来源摸一张牌（至多三张）。",
  ["@!hunting"] = "狩猎",
  ["hunting_trigger"] = "狩猎",
  ["#hunting_skill"] = "标记一名角色，其受到伤害时，令伤害来源摸牌",
}


local deadly_combatSkill = fk.CreateActiveSkill{
  name = "deadly_combat_skill",
  mod_target_filter = function(self, to_select, selected, user)
    return user.id ~= to_select
  end,
  target_filter = Util.TargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local from = room:getPlayerById(effect.from)
    if to.dead or from.dead then return end
    local responsers = { to, from }
    local currentTurn = 1
    local currentResponser = from

    while from:isAlive() and to:isAlive() do
      local loopTimes = 1
      if effect.fixedResponseTimes then
        local canFix = currentResponser == to
        if effect.fixedAddTimesResponsors then
          canFix = table.contains(effect.fixedAddTimesResponsors, currentResponser.id)
        end

        if canFix then
          if type(effect.fixedResponseTimes) == 'table' then
            loopTimes = effect.fixedResponseTimes["slash"] or 1
          elseif type(effect.fixedResponseTimes) == 'number' then
            loopTimes = effect.fixedResponseTimes
          end
        end
      end

      local newResponser = responsers[currentTurn]

      local cardResponded
      for i = 1, loopTimes do
        cardResponded = room:askForResponse(currentResponser, "deadly_combat", "slash", "#deadly_combat-slash:"..newResponser.id, true, nil, effect)
        if cardResponded then
          room:responseCard({
            from = currentResponser.id,
            card = cardResponded,
            responseToEvent = effect,
          })
        else
          break
        end
      end
      if cardResponded and newResponser:isAlive() then
        room:damage({
          from = currentResponser,
          to = newResponser,
          card = effect.card,
          damage = 1,
          damageType = fk.NormalDamage,
          skillName = self.name,
        })
      else
        break
      end
      currentResponser = newResponser

      currentTurn = currentTurn % 2 + 1
    end

  end
}
local deadly_combat = fk.CreateTrickCard{
  name = "deadly_combat",
  suit = Card.Spade,
  number = 2,
  is_damage_card = true,
  skill = deadly_combatSkill,
  special_skills = { "emo_zhujian&" },
}

extension:addCards({
  deadly_combat,
})

Fk:loadTranslationTable{
  ["deadly_combat"] = "死斗",
  [":deadly_combat"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：一名其他角色<br /><b>效果</b>：由你开始，你与目标角色轮流：打出一张【杀】并对对方造成1点伤害，否则结束此牌结算。",
  ["#deadly_combat-slash"] = "死斗：打出一张【杀】，对 %src 造成1点伤害！",
}


local ambush_skill = fk.CreateActiveSkill{
  name = "ambush_skill",
  mod_target_filter = function(self, to_select, selected, user)
    local to = Fk:currentRoom():getPlayerById(to_select)
    if to == user then return false end
    return #to:getEquipments(Card.SubtypeArmor) > 0 or #to:getEquipments(Card.SubtypeDefensiveRide) > 0
  end,
  target_filter = Util.TargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local from = room:getPlayerById(effect.from)
    if to.dead or from.dead then return end
    local equips = to:getEquipments(Card.SubtypeArmor)
    table.insertTable(equips, to:getEquipments(Card.SubtypeDefensiveRide))
    if equips and room:askForSkillInvoke(to, self.name, nil, "#ambush-give:"..from.id) then
      room:obtainCard(from, equips, true, fk.ReasonPrey, from.id, self.name)
    else
      room:damage { from = from, to = to, damage = 1, card = effect.card , skillName = self.name }
    end
  end
}

local ambush = fk.CreateTrickCard{
  name = "ambush",
  suit = Card.Spade,
  number = 9,
  is_damage_card = true,
  skill = ambush_skill,
  special_skills = { "emo_zhujian&" },
}

extension:addCards({
  ambush,
})

Fk:loadTranslationTable{
  ["ambush"] = "伏击",
  [":ambush"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：装备区有防具或防御坐骑的一名其他角色<br /><b>效果</b>：目标角色选一项：1.你获得其装备区的防具和防御坐骑；2.你对其造成1点伤害。",
  ["#ambush-give"] = "伏击：“确定”:%src 获得你装备的防具和防御坐骑；“取消”:其对你造成1点伤害",
  ["ambush_skill"] = "伏击",
}


local blindbox_skill = fk.CreateActiveSkill{
  name = "blindbox_skill",
  mod_target_filter = Util.TrueFunc,
  can_use = Util.SelfCanUse,
  on_use = function(self, room, cardUseEvent)
    if not cardUseEvent.tos or #TargetGroup:getRealTargets(cardUseEvent.tos) == 0 then
      cardUseEvent.tos = { { cardUseEvent.from } }
    end
  end,
  on_effect = function(self, room, effect)
    local player = room:getPlayerById(effect.to)
    local cards = room:getNCards(3)
    room:moveCards({ ids = cards, toArea = Card.Processing, moveReason = fk.ReasonPut , skillName = self.name, proposer = player.id })
    room:delay(600)
    for _, id in ipairs(cards) do
      if player.dead then break end
      local card = Fk:getCardById(id)
      local tos = U.getDefaultTargets(player, card, true, true)
      if tos then
        room:useCard{
          from = player.id,
          tos = table.map(tos, function(p) return {p} end),
          card = card,
          extraUse = true, 
        }
        room:delay(300)
      end
    end
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    if #cards > 0 then
      room:moveCards({ ids = cards, toArea = Card.DiscardPile, moveReason = fk.ReasonPutIntoDiscardPile })
    end
  end
}
local blindbox = fk.CreateTrickCard{
  name = "blindbox",
  suit = Card.Diamond,
  number = 8,
  skill = blindbox_skill,
}

extension:addCards({
  blindbox,
})

Fk:loadTranslationTable{
  ["blindbox"] = "盲盒",
  [":blindbox"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：你<br /><b>效果</b>：目标角色亮出牌堆顶三张牌，对随机角色使用其中可使用的牌（无距离和次数限制且不计入次数）。",
  ["blindbox_skill"] = "盲盒",
  ["#blindbox_skill"] = "你亮出牌堆顶三张牌，对随机角色使用其中的牌",
}


local attract_lightning_skill = fk.CreateActiveSkill{
  name = "attract_lightning_skill",
  prompt = "#attract_lightning_skill",
  mod_target_filter = Util.TrueFunc,
  target_filter = Util.TargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local player = room:getPlayerById(effect.from)
    
    while not to.dead do
      local judge = {
        who = to,
        reason = "attract_lightning",
        pattern = ".|.|spade,club",
      }
      room:judge(judge)
      if judge.card.color == Card.Black then
        if not player.dead and not player:isNude() then
          if #room:askForDiscard(player, 1, 1, true, self.name, true, ".|.|spade,club", "#attract_lightning-damage:"..to.id) > 0 then
            room:damage { from = player, to = to, damage = 1, card = effect.card, skillName = self.name, damageType = fk.ThunderDamage }
          end
        end
        break
      else
        if player.dead or player:isNude() or to.dead or 
        #room:askForDiscard(player, 1, 1, true, self.name, true, ".|.|heart,diamond", "#attract_lightning-repeat:"..to.id) == 0 then
          break
        end
      end
    end
  end
}


local attract_lightning = fk.CreateTrickCard{
  name = "attract_lightning",
  suit = Card.Spade,
  number = 10,
  skill = attract_lightning_skill,
  is_damage_card = true,
}

extension:addCards({
  attract_lightning,
})

Fk:loadTranslationTable{
  ["attract_lightning"] = "引雷",
  [":attract_lightning"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：一名角色<br /><b>效果</b>：令目标角色进行一次判定，若判定结果为黑色，你可以弃置一张黑色牌并对其造成1点雷点伤害，若不为黑色，你可以弃置一张红色牌并重复此判定。",
  ["#attract_lightning_skill"] = "引雷：令一名角色判定，若为黑色，你可以弃黑色牌对其造成雷电伤害",
  ["#attract_lightning-damage"] = "引雷：你可以弃置一张黑色牌，对 %src 造成1点雷点伤害",
  ["#attract_lightning-repeat"] = "引雷：你可以弃置一张红色牌，重复对%src 的【引雷】判定",
  ["attract_lightning_skill"] = "引雷",
}


local volcanic_eruption_skill = fk.CreateActiveSkill{
  name = "volcanic_eruption_skill",
  can_use = Util.AoeCanUse,
  on_use = Util.AoeOnUse,
  prompt = "#volcanic_eruption_skill",
  mod_target_filter = function(self, to_select, selected, user, card, distance_limited)
    return user.id ~= to_select
  end,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local from = room:getPlayerById(effect.from)
    if to.dead then return end
    local judge = {
      who = to,
      reason = self.name,
    }
    room:judge(judge)
    room:setCardEmotion(judge.card.id, "./packages/moepack/image/anim/question")
    room:delay(400)
    if to.dead then return end
    local suit = judge.card:getSuitString()
    if #room:askForDiscard(to, 1, 1, false, self.name, true, ".|.|"..suit, "#volcanic_eruption-discard::"..from.id..":log_"..suit) == 0 then
      room:damage({
        from = from,
        to = to,
        card = effect.card,
        damage = 1,
        damageType = fk.FireDamage,
        skillName = self.name,
      })
    end
  end
}

local volcanic_eruption = fk.CreateTrickCard{
  name = "volcanic_eruption",
  suit = Card.Heart,
  number = 13,
  is_damage_card = true,
  multiple_targets = true,
  skill = volcanic_eruption_skill,
}

extension:addCards({
  volcanic_eruption,
})

Fk:loadTranslationTable{
  ["volcanic_eruption"] = "火山爆发",
  ["volcanic_eruption_skill"] = "火山爆发",
  [":volcanic_eruption"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标：</b>所有其他角色<br /><b>效果：</b>目标角色进行判定，其需弃置一张与判定结果花色相同的牌，否则你对其造成1点火焰伤害。",
  ["#volcanic_eruption-discard"] = "火山爆发：弃置一张 %arg 牌，否则 %dest 对你造成1点火焰伤害",
  ["#volcanic_eruption_skill"] = "令所有其他角色依次判定且需弃1张同花色牌，否则受到火焰伤害",
}




return extension
